/*
 * CookController.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.onesalt.domain.Kitchen;
import com.onesalt.service.KitchenService;
import com.onesalt.service.UserService;
import com.onesalt.utils.JSONUtil;

/**
 * 餐厅网络服务
 *
 * @author kiri
 * @date 2015年3月5日
 * @version 1.0
 */
@Controller
@RequestMapping("/kitchens")
public class KitchenController extends AbstractController {

	private final UserService userService;
	
	private final KitchenService kitchenService;

	@Autowired
	public KitchenController(KitchenService kitchenService, UserService userService) {
		this.kitchenService = kitchenService;
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String findAll(@RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize,
			@RequestParam(required = false) Long cookId, @RequestParam(required = false) String cookSn, @RequestParam(required = false) String cookName,
			@RequestParam(required = false) String sn, @RequestParam(required = false) String name,
			@RequestParam(required = false) String city, @RequestParam(required = false) String address,
			@RequestParam(required = false) Double startSpend, @RequestParam(required = false) Integer endSpend,
			@RequestParam(required = false) String applyStartTime, @RequestParam(required = false) String applyEndTime,
			@RequestParam(required = false) String notes, Model model) {

		getLogger().debug("pageIndex = " + pageIndex + ", pageSize = " + pageSize);
		Date parsedApplyStartTime = null;
		Date parsedApplyEndTime = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			parsedApplyStartTime = StringUtils.hasText(applyStartTime) ? sdf.parse(applyStartTime) : null;
			parsedApplyEndTime = StringUtils.hasText(applyEndTime) ? sdf.parse(applyEndTime) : null;
		}
		catch (ParseException ex) {
			getLogger().debug(ex.getMessage(), ex);
		}

		Page<Kitchen> kitchens = getKitchenService().findAll(cookId, cookSn, cookName, sn, name, city, address, startSpend, endSpend, parsedApplyStartTime, parsedApplyEndTime, null, null, notes, pageIndex, pageSize);
		model.addAttribute("page", kitchens);
		List<String> cities = getKitchenService().findAllCities();
		model.addAttribute("cities", cities);

		getLogger().debug("kitchens = " + JSONUtil.toJsonString(kitchens));
		return "kitchen-list";
	}

	@RequestMapping(value = "/{kitchenId}", method = RequestMethod.GET)
	public String findById(@PathVariable long kitchenId, @RequestParam(defaultValue = "false") boolean edit, Model model) {
		Kitchen kitchen = getKitchenService().getKitchenRepository().findOne(kitchenId);
		getLogger().info("find kitchen = " + JSONUtil.toJsonString(kitchen));
		if (kitchen != null) {
			model.addAttribute("kitchen", kitchen);
			model.addAttribute("type", edit ? "edit" : "info");
			return "kitchen";
		}
		else {
			return "error";
		}
	}

	@RequestMapping(value = "/{kitchen}", method = RequestMethod.POST)
	public String update(@ModelAttribute Kitchen kitchen, Model model) {
		Kitchen result = getKitchenService().getKitchenRepository().save(kitchen);
		if (result != null) {
			model.addAttribute("kitchen", result);
			model.addAttribute("type", "info");
			return "kitchen";
		}
		else {
			return "error";
		}
	}

	public KitchenService getKitchenService() {
		return kitchenService;
	}
}
